<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="addVisible = true" type="primary" size="mini"
          >添加分类</el-button
        >
      </div>
      <el-table :data="cateList" stripe border style="width: 100%">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="分类名称" prop="cate_name"> </el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              @click="showEditDialogHandler(scope.row)"
              size="mini"
              type="primary"
              >修改</el-button
            >
            <el-button
              @click="delHandler(scope.row.id)"
              size="mini"
              type="danger"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      title="添加文章分类"
      :visible.sync="addVisible"
      @closed="$refs.addForm.resetFields()"
      width="35%"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="addVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="addCateHandler"
          >添 加</el-button
        >
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="editVisible"
      @closed="$refs.editForm.resetFields()"
      width="35%"
    >
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editForm"
        label-width="70px"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="editVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="editCateHandler"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// vuex 中的数据建议使用辅助函数 mapState 导入
import { mapActions, mapState } from 'vuex'
export default {
  name: 'ArtCate',
  data () {
    return {
      list: [],
      addVisible: false,
      editVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: { cate_name: '', cate_alias: '' },

      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions('artCate', ['getCateList']),
    // async getCateList () {
    //   // 发送 ajax 请求
    //   const { data: res } = await this.$http.get('/my/cate/list')
    //   if (res.code === 0) {
    //     this.list = res.data
    //   }
    // },
    addCateHandler () {
      // 兜底校验
      this.$refs.addForm.validate(async valid => {
        if (!valid) return
        // 校验通过后发请求
        const { data: res } = await this.$http.post(
          '/my/cate/add',
          this.addForm
        )
        // 获取到结果,判断结果提示用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新发起请求获取最新的数据并渲染页面
        this.getCateList()
        // 关闭对话框
        this.addVisible = false
      })
    },
    showEditDialogHandler (row) {
      if (row.id === 1 || row.id === 2) {
        return this.$message.error('管理员不允许修改这个分类')
      }
      // 渲染内部表单数据
      // console.log(row);
      this.editForm = { ...row }
      // 显示编辑的 dialog
      this.editVisible = true
    },
    editCateHandler () {
      // 兜底校验
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        // 校验通过后发请求
        const { data: res } = await this.$http.put(
          '/my/cate/info',
          this.editForm
        )
        // 获取到结果 判断结果提示用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 重新发起请求获取最新的数据并渲染页面
        this.getCateList()
        // 关闭对话框
        this.editVisible = false
      })
    },
    delHandler (id) {
      if (id === 1 || id === 2) {
        return this.$message.error('管理员不允许删除这个分类')
      }
      // 提醒用户是否真的要删除
      this.$confirm('确定要删除吗?呜呜呜', '警告!!!', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发送请求给后台,删除指定的分类并刷新列表
          const { data: res } = await this.$http.delete('/my/cate/del', {
            params: { id }
          }) // 参数1:url,参数2:config 请求配置
          // this.$http({ //参数1:请求配置
          // method:'delete',
          // url:'/my/cate/del',
          // params:{}, //查询参数
          // data:{} //请求体参数
          // })
          // 判断响应结果提示用户
          if (res.code !== 0) return this.$message.error(res.message)
          this.$message.success(res.message)
          // 重新获取最新的分类数据并渲染
          this.getCateList()
        })
        .catch(() => {})
    }
  },
  created () {
    this.getCateList()
  },
  computed: {
    ...mapState('artCate', ['cateList'])
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
